// packageF/silverPointPay/SilverPay/SilverPay.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    step: 1,
    radio: "",
    list: [],
    order_pay_id: "",
    smsMobile: "",
    token: "",
    btnText: "获取验证码",
    timer: null,
    btnTag: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.setNavigationBarTitle({
      title: '快捷支付-银典',
    })
    if (options.order_pay_id) {
      this.setData({
        order_pay_id: options.order_pay_id
      })
    }
    if (options.sn) {
      this.setData({
        sn: options.sn
      })
    }
    if (options.money) {
      this.setData({
        money: options.money
      })
    }
    this.getData();
  },
  radiochange(e) {
    let id = e.detail;
    this.setData({
      radio: id
    })
  },
  next() {
    if (app._isTextEmpty(this.data.radio)) {
      wx.showModal({
        content: '请选择银行卡',
        showCancel: false
      })
      return;
    }
    this.setData({
      step: 2
    })
  },
  confirmBtn() {
    if (app._isTextEmpty(this.data.smsMobile)) {
      wx.showModal({
        content: '请输入验证码',
        showCancel: false
      })
      return;
    }
    this.unionPay();
  },
  checkCodeMethod() {
    let json = {
      order_pay_id: this.data.order_pay_id,
      pay_union_card_id: this.data.radio,

    }
    app._getNetWork({
      url: app.getNetAddresss('order.merge-pay.silver-point-union-pay'),
      data: json,
      success: res => {
        if (res.data.result) {
          this.setData({
            token: res.data.data.pay_msg.token,
          })
          this.sendCodeAnimation();
        } else {
          app.tips(res.data.msg)
        }
      }
    })
  },
  unionPay() {
    let json = {
      ids: this.data.order_pay_id
    };
    if (this.data.sn) {
      json.sn = this.data.sn;
    }
    app._getNetWork({
      url: app.getNetAddresss('plugin.silver-point-pay.frontend.union-card.pay'),
      data: {
        code: this.data.smsMobile,
        token: this.data.token,
        order_pay_id: this.data.order_pay_id
      },
      success: res => {
        if (res.data.result) {
          if (res.data.data.status == '101') {
            this.orderCheck();
          } else {
            wx.navigateTo({
              url: '/packageI/pay_back/pay_back?ids=' + this.data.order_pay_id + '&sn=' + this.data.sn,
            })
          }
        } else {
          app.tips(res.data.msg)
        }
      }
    })
  },
  orderCheck() {
    let json = {
      ids: this.data.order_pay_id
    };
    if (this.data.sn) {
      json.sn = this.data.sn;
    }
    app._getNetWork({
      url: app.getNetAddresss('plugin.silver-point-pay.frontend.union-card.queryOrder'),
      data: {
        pay_sn: this.data.sn,
      },
      success: res => {
        if (res.data.result) {
          wx.navigateTo({
            url: '/packageI/pay_back/pay_back?ids=' + this.data.order_pay_id + '&sn=' + this.data.sn,
          })
        } else {
          app.tips(res.data.msg)
          wx.navigateBack({
            delta: 1,
          })
        }
      }
    })
  },
  sendCodeAnimation() {
    const TIME_COUNT = 60;
    if (!this.data.timer) {
      this.data.count = TIME_COUNT;
      wx.showToast({
        title: '验证码已发送',
        icon: 'success',
        duration: 1000
      })
      this.data.timer = setInterval(() => {
        if (this.data.count > 0 && this.data.count <= TIME_COUNT) {
          this.data.count--;
          this.setData({
            btnTag: true,
            btnText: this.data.count + "s"
          })
        } else {
          this.setData({
            btnTag: false,
            btnText: '获取验证码'
          })
          clearInterval(this.data.timer);
          this.data.timer = null;
        }
      }, 1000);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getData() {
    app._getNetWork({
      url: app.getNetAddresss('plugin.silver-point-pay.frontend.union-card.index'),
      success: res => {
        if (res.data.result) {
          this.setData({
            list: res.data.data,
          })
        } else {
          app.tips(res.data.msg)
        }
      }
    })
  }
})